import javax.jws.WebService;
import javax.naming.InitialContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@WebServlet("Student")
public class DataBase extends HttpServlet {
    protected  void doPost(HttpServletRequest req, HttpServletResponse resp)
    {
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            connection = dataSource.getConnection();
            statement = connection.createStatement();
            statement.executeUpdate( "CREATE TABLE IF NOT EXISTS `Student` (" +
                    "  `id` int(11) unsigned NOT NULL AUTO_INCREMENT," +
                    "`number` int(6) NOT NULL,"+
                    "  `type` varchar(100) NOT NULL," +
                    "  `score` int(11) NOT NULL," +
                    "  PRIMARY KEY (`id`)" +
                    ");");

            statement.executeUpdate(
                    "insert into `Student` (`number`,`type`,`score`) values (100001, '数据结构', 99);" +
                            "insert into `Student` (`number`,`type`,`score`) values (100002, '计组', 95);"+
                            "insert into `Student` (`number`,`type`,`score`) values (100003, 'JAVAEE', 100);"
            );
            resultSet= statement.executeQuery("select * from Student ORDER BY id DESC LIMIT 0,3");
            resp.setContentType("text/html;charset=UTF-8");
            while(resultSet.next()){
                resp.getWriter().println("学号:"+resultSet.getInt("number")+"科目:"+resultSet.getString("type")+"成绩:"+resultSet.getInt("score")+"<br>");
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    {
        doPost(req,resp);
    }
}
